Including images into Quarto

Session - Images

Zoë Turner

Adding images using markdown

The NHS-R Community logo taken from the GitHub repository directly

![](https://raw.githubusercontent.com/nhs-r-community/assets/main/logo/nhsr-logo.svg)

Adding images using html

<a href='https://nhsrcommunity.com/'><img src='https://raw.githubusercontent.com/nhs-r-community/assets/main/logo/nhsr-logo.svg' align="right" height="80" /></a>

Adding images using {knitr}

knitr::include_graphics("img/purrr_cat.png")

Adding images using the Visual view in RStudio

The Visual editor is great for WSIWYG (what you see is what you get) but also really useful for those who prefer the Markdown coding as the wizards give prompts:

  • In the Visual view click on the picture image in the tool bar before Format and Insert
  • Browse to the image you need
  • Then go back to Source to look at what code is generated

Let’s try this together!

  1. Open `images_report/example_report.qmd
  2. In the source view we’ll link to an image in the same folder
  3. Then link to an image in a subfolder
  4. Finishing with a link to an image in a subfolder from the main root

Some helpful tips

  • Add embed-resources: true to the YAML to remove the generated folder
  • The code we are testing is on the previous slide…

Note that autofills. To force a prompt to autofill use tab

Also if Global Options/RMarkdown/Show output inline for all R Markdown… is ticked the picture will automatically appear in the script. To remove untick but also requires a restart Ctrl + Shift + F10

Seeing the image in the script

Using the drop down menu with a cog, found next to the blue arrow with Render

Preview Images and Equations

will not be ticked, selecting this will allow Quarto to show the image in the report script.

Alt text

Alternative text is predominately for people using screen readers to explain images but is very useful when images break:

File should be baker_3.png

![](img/bakers3.png){fig-alt="Three green fuzzy monsters in chef hats stand on top of one another, with the one on top pouring flour into a mixing bowl."}

Rendered image doesn’t work but the text shows

Three green fuzzy monsters in chef hats stand on top of one another, with the one on top pouring flour into a mixing bowl.

Alt text using Markdown

Between {} that comes after the link:

![](img/bakers_3.png){fig-alt="Three green fuzzy monsters in chef hats stand on top of one another, with the one on top pouring flour into a mixing bowl."}

Three green fuzzy monsters in chef hats stand on top of one another, with the one on top pouring flour into a mixing bowl.

Alt text using HTML

Within the <> and space between sections

<div id="bakeoff">
  <img src="img/bakers_3.png" alt="Alt text:Three green fuzzy monsters in chef hats stand on top of one another, with the one on top pouring flour into a mixing bowl.">
</div> 

Alt text:Three green fuzzy monsters in chef hats stand on top of one another, with the one on top pouring flour into a mixing bowl.

Alt text using {knitr}

Code used in R chunks

```{r}
#| eval: false
#| fig-alt: "Three green fuzzy monsters in chef hats stand on top of one another, with the one on top pouring flour into a mixing bowl."

knitr::include_graphics("img/bakers_3.png")
```

Three green fuzzy monsters in chef hats stand on top of one another, with the one on top pouring flour into a mixing bowl.

Resizing images

Markdown doesn’t support resizing and html requires knowledge of a different language but using {knitr} and Quarto options:

```{r}
#| out-width: "50%"

knitr::include_graphics("img/bakers_3.png")
```

Centre images

And can change where the image appears:

```{r}
#| out-width: "30%"
#| fig-align: center

knitr::include_graphics("img/bakers_3.png")
```

Quiz - 1

How do you add headers in Markdown?

! Header

- Header

# Header

1. Header

Answer: Headers is # hash

Quiz - 2

What about lists? Bulleted? Numbered?

! Item 1

- Item 1

# Item 1

1. Item 1

Lists are - for bullets and 1. for numbered

Next Section

Artwork from Allison Horst “Illustrations from Hadley Wickham’s ACM talk”The Joy of Functional Programming (for Data Science).” https://allisonhorst.com/wickham-tidy-bakers